package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysRoleUser;
import cn.wgx.modules.sys.entity.SysRoleUserExample;
import cn.wgx.modules.sys.entity.mapper.SysRoleUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SysRoleUserService extends BaseService<SysRoleUser> {

    @Resource
    SysRoleUserMapper mapper;

    @Transactional(readOnly = true)
    public List<SysRoleUser> getList(SysRoleUser record, Boolean desc) {
		SysRoleUserExample example = new SysRoleUserExample();
		example.setOrderByClause("createDate " + (desc?"desc": "asc"));
		return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysRoleUser> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysRoleUserExample example = new SysRoleUserExample();
            SysRoleUserExample.Criteria criteria = example.createCriteria();
            criteria.andRole_user_idIn(uniList);
            List<SysRoleUser> eduCourseAssignmentHis = mapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    @Transactional(readOnly = true)
    public SysRoleUser getOne(Integer id) {
		return mapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    public List<SysRoleUser> getListByUserId(Integer userId) {
        SysRoleUserExample example = new SysRoleUserExample();
        example.createCriteria().andCreateUserIdEqualTo(userId);
        return mapper.selectByExample(example);
    }


    @Transactional
    public SysRoleUser insertOne(SysRoleUser record) {
        mapper.insertSelective(record);
        return record;
    }

    @Transactional
    public SysRoleUser insertOne(Integer user_id, Integer role_id) {
        if(Validation.isNotNullOrZero(role_id) && Validation.isNotNullOrZero(user_id)){
            SysRoleUser sysRoleUser = new SysRoleUser();
            sysRoleUser.setRole_id(role_id);
            sysRoleUser.setUser_id(user_id);
            filter(sysRoleUser, true);
            mapper.insertSelective(sysRoleUser);
            return sysRoleUser;
        }
        return null;
    }


    @Transactional
    public SysRoleUser updateOne( SysRoleUser record) {
        filter(record, false);
        mapper.updateByPrimaryKeySelective(record);
        return record;

    }


    @Transactional
    public int deleteOne( Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

	

}